package com.fernandomdo.ServiceAdministrator;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class CustomImageButton extends View {
	private static final int WIDTH_PADDING = 8;
	private static final int HEIGHT_PADDING = 10;
	private final String label;
	private final int imageResId;
	private final Bitmap image;
	private final InternalListener listenerAdapter = new InternalListener();
	
	/**
	 * Constructor
	 * 
	 * @param context
	 *		Activity context in which the button view is being placed for.
	 * 
	 * @param resImage
	 * 		Image to put on the button. This image should have been placed in the drawable resources directory.
	 * 
	 * @param label
	 * 		The text label to display for the custom button
	 */
	public CustomImageButton(Context context, int resImage, String label) 
	{
		super(context);
		this.label = label;
		this.imageResId = resImage;
		this.image = BitmapFactory.decodeResource(context.getResources(), imageResId);
		
		setFocusable(true);
		setBackgroundColor(Color.WHITE);
		setOnClickListener(listenerAdapter);
		setClickable(true);
	}
	
	/**
	 * The method that is called when the focus is changed to or from this view.
	 */
	@Override
	protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) 
	{
		if(gainFocus == true)
		{
			this.setBackgroundColor(Color.rgb(255, 165, 0));
		}
		else 
		{
			this.setBackgroundColor(Color.WHITE);
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		Paint textPaint = new Paint();
		textPaint.setColor(Color.BLACK);
		canvas.drawBitmap(image, WIDTH_PADDING / 2, HEIGHT_PADDING / 2, null);
		canvas.drawText(label, WIDTH_PADDING / 2, (HEIGHT_PADDING / 2) + image.getHeight() + 8, textPaint);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
	}
	
	private int measureWidth(int measureSpec)
	{
		int preferred = image.getWidth() * 2;
		return getMeasurement(measureSpec, preferred);
	}
	
	private int measureHeight(int measureSpec) 
	{
		int preferred = image.getHeight() * 2;
		return getMeasurement(measureSpec, preferred);
	}
	
	private int getMeasurement(int measureSpec, int preferred)
	{
		int specSize = MeasureSpec.getSize(measureSpec);
		int measurement = 0;
		
		switch(MeasureSpec.getMode(measureSpec))
		{
		case MeasureSpec.EXACTLY:
			//The width of the view has been given
			measurement = specSize;
			break;
		case MeasureSpec.AT_MOST:
			//Take the min of the preferred size and what we were told to be
			measurement = Math.min(preferred, specSize);
			break;
		default:
			measurement = preferred;
			break;
		}
		
		return measurement;
	}
	
	/**
	 * Sets the listener object that is triggered when the view is clicked
	 * 
	 * @param newListener
	 * 		The instance of the listener to trigger
	 * 
	 */
	@Override
	public void setOnClickListener(OnClickListener newListener)
	{
		listenerAdapter.setListener(newListener);
	}
	
	/**
	 * Returns the resoure id of the image
	 */
	public int getImageResId()
	{
		return imageResId;
	}
	
	/**
	 * Internal click listener class. Translates a view's click listener into 
	 *  one that is more appropriate for the custom image button class.
	 */
	private class InternalListener implements View.OnClickListener
	{
		private OnClickListener listener = null;
		
		/**
		 * Changes the listener to the given listener
		 * 
		 * @param newListener
		 * 		The listener to change to.
		 */
		public void setListener(OnClickListener newListener)
		{
			listener = newListener;
		}
		
		//@Override
		@Override
		public void onClick(View v)
		{
			if(listener != null)
			{
				listener.onClick(CustomImageButton.this);
			}
		}
	}
}
